//
//  JLServiceWebViewController.m
//  JiuLe
//
//  Created by xcq on 15/12/14.
//  Copyright © 2015年 Fine. All rights reserved.
//

#import "JLServiceWebViewController.h"
#import "BaseDataRequest.h"


@interface JLServiceWebViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *developerImg;
@property (strong, nonatomic) NSString *homeURL;
@property (strong, nonatomic) NSString *otherURL;
@end

@implementation JLServiceWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"挂号";
    [self showNavgationWithLeftItemWithTitle:@"返回" twoItemWithtitle:@"╳"];//❌
    _webView.delegate = self;
    if (_isDeveloper) {
        _developerImg.hidden = NO;
        _webView.hidden = YES;
    } else {
        [self getEncryptedData];
    }
    _homeURL = _otherURL = @"";
}

- (void)getEncryptedData{
    [SVProgressHUD showWithStatus:@"加载中.." maskType:SVProgressHUDMaskTypeClear];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    [[BaseDataRequest alloc] getEncryptedData:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        if ([status isEqualToString:@"0"]) {
            NSString *strURL = [[responseObject objectForKey:@"result"] objectForKey:@"encrypt"];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                            initWithURL:[NSURL URLWithString:strURL]
                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:15.f];
            [_webView loadRequest:request];
            
        }else{
            [SVProgressHUD dismiss];
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
        }
    }];
}


- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setHidesBottomBarWhenPushed:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
    [self.navigationController setHidesBottomBarWhenPushed:NO];
}

#pragma mark - UIWebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"%@",error);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
    if ([_homeURL isEqualToString:@""] || _homeURL == nil) {
        _homeURL =  webView.request.URL.absoluteString;
        NSLog(@"%@",_homeURL);
    }
    _otherURL =  webView.request.URL.absoluteString;
    NSLog(@"%@",_homeURL);
    NSLog(@"%@",_otherURL);
}

- (void)backTo{
    if ([_homeURL isEqualToString:_otherURL]) {
        if (self.navigationController) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        [_webView goBack];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
